package com.gopay.common.cps.dao.order.impl;

import com.gopay.common.base.dao.impl.BaseReadDAOImpl;
import com.gopay.common.cps.dao.order.FundCpsGeneralMainOrderDao;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author baiyunpeng
 * @2016年7月14日 下午3:36:05  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("fundCpsMainDao")
public class FundCpsGeneralMainOrderDaoImpl extends BaseReadDAOImpl<CpsGenMainOrder,Serializable> implements FundCpsGeneralMainOrderDao {

    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(FundCpsGeneralMainOrderDaoImpl.class);

    /**
     * 批量插入主订单记录
     * @param connection
     * @param orderList
     * @throws SQLException
     */
    @Override
     public void batchInsertMainOrders(Connection connection, final List<CpsGenMainOrder> orderList) throws SQLException{
        // 插入sql
        StringBuilder sql = new StringBuilder("insert into CPS_GEN_MAIN_ORDER (");
        StringBuilder values=new StringBuilder("");
        sql.append("GOPAY_ORDER_ID,");values.append("?,");
        sql.append("TRANS_DTL_SQ,");values.append("?,");
        sql.append("GOPAY_INT_TXN_CD,");values.append("?,");
        sql.append("MER_ID,");values.append("?,");
        sql.append("MER_ORDER_ID,");values.append("?,");
        sql.append("MER_TXN_TM,");values.append("?,");
        sql.append("GATEWAY_SOURCE,");values.append("?,");
        sql.append("GATEWAY_CHANNEL,");values.append("?,");
        sql.append("ORDER_TYPE,");values.append("?,");
       //国付宝生成订单时间
        sql.append("GOPAY_TXN_TM,");values.append("?,");
        sql.append("TOTAL_ORDER_AMT,");values.append("?,");
        sql.append("TOTAL_ORDER_FEE_AMT,");values.append("?,");
        sql.append("ORDER_DEAL_AMT,");values.append("?,");
        sql.append("ORDER_REAL_FEE_AMT,");values.append("?,");
        sql.append("CREATED_TS,");values.append("?,");
        sql.append("LAST_UPD_TS,");values.append("?,");
        sql.append("MER_TXN_AMT,");values.append("?,");
         sql.append("ORDER_STLM_TIME,");values.append("?,");
         sql.append("TRAN_FINISH_TIME,");values.append("?,");
         sql.append("REMARK1,");values.append("?,");
         sql.append("CUR_TYPE,");values.append("?,");
         sql.append("PAY_CHANNEL,");values.append("?,");
         sql.append("ORDER_FEE_PAYER,");values.append("?,");
         sql.append("PAY_CUST_ID,");values.append("?,");
         sql.append("PAY_ACCT,");values.append("?,");
         sql.append("VERSION,");values.append("?,");
         sql.append("SETTLE_AMT,");values.append("?,");
        sql.append("TXN_STA_CD");values.append("?");
        sql.append(" ) values ( ");
        sql.append(values);
        sql.append(" ) ");

        PreparedStatement pst = connection.prepareStatement(sql.toString());
        try {
            for (int i = 0; i < orderList.size(); i++) {
                CpsGenMainOrder order = orderList.get(i);

                //GOPAY_ORDER_ID
                pst.setString(1, order.getGopayOrderId());

                //TRANS_DTL_SQ
                pst.setInt(2, order.getTransDtlSq());

                //GOPAY_INT_TXN_CD
                pst.setString(3, order.getGopayIntTxnCd());

                //MER_ID
                pst.setString(4, order.getMerId());

                //MER_ORDER_ID
                pst.setString(5, order.getMerOrderId());

                //MER_TXN_TM
                pst.setTimestamp(6, new Timestamp(order.getMerTxnTm().getTime()));

                //GATEWAY_SOURCE
                pst.setString(7, order.getGatewaySource());

                //GATEWAY_CHANNEL
                pst.setString(8, order.getGatewayChannel());

                //ORDER_TYPE
                pst.setString(9, order.getOrderType());

                //GOPAY_TXN_TM
                pst.setTimestamp(10, new Timestamp(order.getGopayTxnTm().getTime()));

                //TOTAL_ORDER_AMT
                pst.setBigDecimal(11, order.getTotalOrderAmt());

                //TOTAL_ORDER_FEE_AMT
                pst.setBigDecimal(12, order.getTotalOrderFeeAmt());

                //ORDER_DEAL_AMT
                pst.setBigDecimal(13, order.getOrderDealAmt());

                //ORDER_REAL_FEE_AMT
                pst.setBigDecimal(14, order.getOrderRealFeeAmt());

                //CREATED_TS
                pst.setTimestamp(15, new Timestamp(order.getCreatedTs().getTime()));

                //LAST_UPD_TS
                pst.setTimestamp(16, new Timestamp(order.getLastUpdTs().getTime()));

                pst.setBigDecimal(17, order.getMerTxnAmt());

                Date orderStlmTime = order.getOrderStlmTime();
                if(orderStlmTime!=null) {
                    pst.setTimestamp(18,new Timestamp(orderStlmTime.getTime()));
                } else {
                    pst.setTimestamp(18,null);
                }
                Date tranFinishTime = order.getTranFinishTime();
                if(tranFinishTime!=null) {
                    pst.setTimestamp(19, new Timestamp(tranFinishTime.getTime()));
                } else {
                    pst.setTimestamp(19,null);
                }
                pst.setString(20,order.getRemark1());
                pst.setString(21,order.getCurType());
                pst.setString(22,order.getPayChannel());
                pst.setString(23,order.getOrderFeePayer());
                pst.setString(24,order.getPayCustId());
                pst.setString(25,order.getPayAcct());
                pst.setInt(26,order.getVersion());
                pst.setBigDecimal(27,order.getSettleAmt());
                pst.setInt(28,order.getTxnStaCd());

                // 把一个SQL命令加入命令列表
                pst.addBatch();
            }
            // 执行批量插入
            pst.executeBatch();
        } catch (SQLException e) {
            LOGGER.error("批量插入主订单记录异常",e);
            throw e;
        } finally {
            if (pst!=null){
                pst.close();
            }
        }
    }
}
